﻿Imports Microsoft.SqlServer.Dts.Runtime
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Public Class SMSTaskForm

    Private Services As SSISComponents.SMSTask.Services
    Private intTextLength As Integer = 160
    Private m_taskHost As TaskHost
    Private Service As SSISComponents.SMSTask.IService
    Private Type As DataTable
    Private strParameter As String = Nothing
    Private dtParameter As DataTable
    Private ServiceURL As String = "http://www.ssis-components.net"
    Private ServiceHelp As String = Nothing

    Public Sub New(ByVal th As TaskHost, ByVal connection As Connections)
        Me.InitializeComponent()
        m_taskHost = th
        Try
            If Not IsNothing(m_taskHost.Properties("ServiceName").GetValue(m_taskHost)) Then
                Me.txtUsername.Text = m_taskHost.Properties("Username").GetValue(m_taskHost)
                Me.txtPassword.Text = m_taskHost.Properties("Password").GetValue(m_taskHost)
                Me.txtRufnummer.Text = m_taskHost.Properties("Recipient").GetValue(m_taskHost)
                Me.txtSMSText.Text = m_taskHost.Properties("Text").GetValue(m_taskHost)
                Me.txtDate.Text = m_taskHost.Properties("Delay").GetValue(m_taskHost)
                Me.txtAltDisplayText.Text = m_taskHost.Properties("Sender").GetValue(m_taskHost)
                Me.cbHTTPS.Checked = m_taskHost.Properties("HTTPS").GetValue(m_taskHost)
                Me.strParameter = m_taskHost.Properties("Parameter").GetValue(m_taskHost)
                dtParameter = SSISComponents.SMSTask.Services.DeserializeObject(strParameter)
            Else
                dtParameter = Service.AdditionalParameter.Copy
            End If
            Me.dgProviderParameter.DataSource = dtParameter
            Me.dgProviderParameter.Columns(0).Width = 160
            Me.dgProviderParameter.Columns(1).Width = 57
            Me.dgProviderParameter.Columns(2).Width = 160
        Catch ex As Exception
        End Try
    End Sub

    Private Sub cbSMSType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbSMSType.SelectedIndexChanged
        Try
            Me.intTextLength = Type.Rows(Me.cbSMSType.SelectedIndex)("Length")
            Me.txtLength.Text = Type.Rows(Me.cbSMSType.SelectedIndex)("Length")
            Me.txtAltDisplayText.Enabled = Type.Rows(Me.cbSMSType.SelectedIndex)("Sender")
            CalculateText()
        Catch
        End Try
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        SetService()
        m_taskHost.Properties("Username").SetValue(m_taskHost, Service.Username)
        m_taskHost.Properties("Password").SetValue(m_taskHost, Service.Password)
        m_taskHost.Properties("ServiceName").SetValue(m_taskHost, Service.Name)
        m_taskHost.Properties("Recipient").SetValue(m_taskHost, Service.Recipient)
        m_taskHost.Properties("Sender").SetValue(m_taskHost, Service.Sender)
        m_taskHost.Properties("Text").SetValue(m_taskHost, Service.Text)
        m_taskHost.Properties("Delay").SetValue(m_taskHost, Service.Delay)
        m_taskHost.Properties("HTTPS").SetValue(m_taskHost, Service.HTTPS)
        m_taskHost.Properties("Type").SetValue(m_taskHost, Service.Type)
        m_taskHost.Properties("Parameter").SetValue(m_taskHost, Service.Parameter)
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub txtSMSText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSMSText.TextChanged
        CalculateText()
    End Sub

    Private Sub CalculateText()
        Me.txtLength.Text = intTextLength - Me.txtSMSText.Text.Length
        If (intTextLength - Me.txtSMSText.Text.Length) < 0 Then
            Me.txtLength.BackColor = Color.Red
        Else
            Me.txtLength.BackColor = Color.LightGray
        End If
    End Sub

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
        Try
            SetService()
            Dim strMessage As String = Service.Send
            If strMessage <> "" And Not IsNothing(strMessage) Then MsgBox(strMessage)
        Catch ex As Exception
            MsgBox("An unexpected error occurred.")
        End Try
    End Sub

    Private Sub btnInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInfo.Click
        Try
            SetService()
            Dim strMessage As String = Service.Info
            If strMessage <> "" And Not IsNothing(strMessage) Then MsgBox(strMessage)
        Catch ex As Exception
            MsgBox("An unexpected error occurred.")
        End Try
    End Sub

    Private Sub SMSTaskForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Services = New SSISComponents.SMSTask.Services
        Me.cbServices.DataSource = Services.Names
        Try
            If Not IsNothing(m_taskHost.Properties("Name").GetValue(m_taskHost)) Then
                Me.cbServices.SelectedIndex = Me.cbServices.Items.IndexOf(m_taskHost.Properties("ServiceName").GetValue(m_taskHost))
                Me.cbSMSType.SelectedValue = m_taskHost.Properties("Type").GetValue(m_taskHost)
                dtParameter = SSISComponents.SMSTask.Services.DeserializeObject(strParameter)
                Me.dgProviderParameter.DataSource = dtParameter
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub cbServices_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbServices.SelectedIndexChanged
        For Each _service As SSISComponents.SMSTask.IService In Services.IServices
            If _service.Name = cbServices.SelectedValue Then
                Me.Service = _service
                Me.Type = Service.Types.Get
                Me.cbSMSType.DataSource = Type
                Me.cbSMSType.DisplayMember = "Name"
                Me.cbSMSType.ValueMember = "Value"
            End If
        Next
        SetHelp()
        SetNewParameter()
    End Sub

    Private Sub SetNewParameter()
        dtParameter = Service.AdditionalParameter.Copy()
        Me.dgProviderParameter.DataSource = dtParameter
        Me.dgProviderParameter.Columns(0).Width = 160
        Me.dgProviderParameter.Columns(1).Width = 57
        Me.dgProviderParameter.Columns(2).Width = 160
    End Sub

    Private Sub SetService()
        Service.Username = Me.txtUsername.Text
        Service.Password = Me.txtPassword.Text
        Service.Recipient = Me.txtRufnummer.Text
        Service.Type = Me.cbSMSType.SelectedValue
        Service.Text = Me.txtSMSText.Text
        Service.Delay = Me.txtDate.Text
        Service.Sender = Me.txtAltDisplayText.Text
        Service.HTTPS = Me.cbHTTPS.Checked
        Service.Parameter = SSISComponents.SMSTask.Services.SerializeObject(dtParameter)
    End Sub

    Private Sub SetHelp()
        Me.lblServiceName.Text = Service.Name
        Me.linkServiceURL.Text = ServiceURL
        Me.ServiceURL = ServiceURL
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        System.Diagnostics.Process.Start("http://smstask.codeplex.com/")
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        System.Diagnostics.Process.Start("http://www.datenfabrik.com")
    End Sub

    Private Sub LinkLabel3_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
        System.Diagnostics.Process.Start("http://www.ssis-components.net")
    End Sub

    Private Sub linkServiceURL_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linkServiceURL.LinkClicked
        System.Diagnostics.Process.Start(ServiceURL)
    End Sub

End Class
